<?php  
// 数据库配置  
$servername = "localhost"; // MySQL服务器名称，通常是localhost或数据库服务器的IP地址  
$username = "root"; // 数据库用户名  
$password = "root"; // 数据库密码  
$dbname = "music"; // 数据库名  
  

$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}

// 假设你已经通过POST方法接收到了注册数据
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // 验证输入
    $name = $conn->real_escape_string($_POST['name']);
    $email = $conn->real_escape_string($_POST['email']);
    $password = $_POST['password']; // 原始密码
    $sex = $conn->real_escape_string($_POST['sex']);
    $phone = $conn->real_escape_string($_POST['phone']);
    
    // 对密码进行哈希处理
    $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
    

    // 插入新用户到数据库
    $sql = "INSERT INTO users (name, password, sex, email, phone) VALUES ('$name', '$hashedPassword', '$sex', '$email', '$phone')";
    
    if ($conn->query($sql) === TRUE) {
        // 注册成功，重定向到login.php页面
        header("Location: login.php");
        exit(); // 确保重定向后停止脚本执行
    } else {
        echo "注册失败: " . $conn->error;
    }
}

// 关闭连接
$conn->close();
?>
  
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册</title>
    <link rel="stylesheet" type="text/css" href="register.css">
    <script src="jQuery/jquery-3.6.0.min.js"></script>
</head>

<body>
    <div class="top">
        <div style="width: 980px;">
            <a class="biaoti" href="homepage.php">宇宙音乐</a>
            <a class="suoyin">评论</a>
            <a class="suoyin">合作</a>
            <a class="suoyin">客服</a>
            <a class="suoyin">我的购买</a>
        </div>
    </div>



<div class="middle"> 
   <h2>注册</h2>  
    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">  
        <div>  
            <p><label for="name">用户名:</label></p>  
            <input type="text" name="name" required>  
        </div>  
        
        <div>  
            <p><label for="email">电子邮箱:</label></p>  
            <input type="email" name="email" required>  
        </div> 
         
        <div>  
            <p><label for="password">密码:</label></p>  
            <input type="password" name="password" required>  
        </div>
          
        <div>  
            <p><label for="sex">性别:</label></p>  
            <select name="sex" required>  
                <option value="male">男</option>  
                <option value="female">女</option>  
                <option value="other">其他</option>  
            </select>  
        </div>  
        
        <div>  
           <p> <label for="phone">手机号码:</label></p>  
            <input type="tel" name="phone" pattern="[0-9]{10,15}" required>  
        </div>
        
          
        <div>  
            <input type="submit" value="注册">  
        </div>  
    </form>  
    <div class="bottom">
</div>  
    
    
    
    
    
        <p>
            <a></a>树立版权意识，支持正版音乐！
        </p>
        <hr><br>
        <p>关于 宇宙音乐\推广合作请联系电子邮箱：2333333333@google.com</p>
    </div>
</body>
